// index.js
Page({
  data: {
    // 移除所有用户信息相关数据
    showFeedback: false,
    feedbackText: '',
    contactType: 'email', // email/phone
    contactInfo: ''
  },

  onLoad() {
    this.checkCameraAuth();
  },

  checkCameraAuth() {
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.camera'] || !res.authSetting['scope.writePhotosAlbum']) {
          this.requestCameraAuth();
        }
      }
    });
  },

  requestCameraAuth() {
    wx.authorize({
      scope: 'scope.camera',
      success: () => {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          fail: () => this.showAuthTips('相册')
        });
      },
      fail: () => {
        this.showAuthTips('相机');
      }
    });
  },

  showAuthTips(type) {
    wx.showModal({
      title: '权限提示',
      content: `需要${type}权限才能使用扫码功能，是否去设置？`,
      success: (res) => {
        if (res.confirm) {
          wx.openSetting();
        }
      }
    });
  },

  goToScan() {
    wx.scanCode({
      onlyFromCamera: false,
      scanType: ['qrCode'],
      success: (res) => {
        wx.showLoading({
          title: '验证溯源码',
          mask: true
        });

        // 保留原有验证逻辑
        wx.request({
          url: 'http://107.151.248.212:8000/back/qrcode/verify',
          method: 'POST',
          data: {
            encryptedData: res.result.trim(),
            scanTime: new Date().getTime(),
            scanLocation: "default"
          },
          success: (response) => {
            wx.hideLoading();
            if (response.data.code === 200) {
              const { traceId, batchId } = response.data.data;
              wx.navigateTo({
                url: `/pages/detail/detail?traceId=${traceId}&batchId=${batchId}`
              });
            } else {
              wx.showToast({
                title: response.data.data || '无效的溯源码',
                icon: 'none'
              });
            }
          },
          fail: (error) => {
            wx.hideLoading();
            wx.showToast({
              title: '网络请求失败，请重试',
              icon: 'none'
            });
          }
        });
      },
      fail: (err) => {
        if (err.errMsg.includes('permission')) {
          this.showAuthTips('相册');
        }
      }
    });
  },

  // 显示反馈弹窗
  showFeedbackModal() {
    this.setData({ showFeedback: true });
  },

  // 隐藏反馈弹窗
  hideFeedbackModal() {
    this.setData({ showFeedback: false });
  },

  // 输入反馈内容
  onInputFeedback(e) {
    this.setData({ feedbackText: e.detail.value });
  },

  // 切换联系方式类型
  onContactTypeChange(e) {
    this.setData({
      contactType: e.detail.value,
      contactInfo: '' // 切换时清空输入
    });
  },

  // 输入联系方式
  onContactInput(e) {
    this.setData({ contactInfo: e.detail.value });
  },

  // 提交反馈
  submitFeedback() {
    if (!this.data.feedbackText) {
      wx.showToast({ title: '请填写反馈内容', icon: 'none' });
      return;
    }

    if (!this.data.contactInfo) {
      wx.showToast({ title: '请填写联系方式', icon: 'none' });
      return;
    }

    // 格式验证
    if (this.data.contactType === 'email' && !this.validateEmail()) {
      wx.showToast({ title: '邮箱格式不正确', icon: 'none' });
      return;
    }

    if (this.data.contactType === 'phone' && !this.validatePhone()) {
      wx.showToast({ title: '手机号格式不正确', icon: 'none' });
      return;
    }

    // 提交到服务器
    wx.request({
      url: 'http://107.151.248.212:8000/back/feedback/add',
      method: 'POST',
      data: {
        feedbackText: this.data.feedbackText,
        contactType: this.data.contactType,
        contactInfo: this.data.contactInfo
      },
      success: () => {
        wx.showToast({ title: '提交成功' });
        this.hideFeedbackModal();
      }
    });
  },

  // 邮箱验证
  validateEmail() {
    return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.data.contactInfo);
  },

  // 手机号验证
  validatePhone() {
    return /^1[3-9]\d{9}$/.test(this.data.contactInfo);
  }
});
